package com.homework.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.homework.bean.AnswerBean;
import com.homework.bean.CourseBean;
import com.homework.bean.QuestionBean;
import com.homework.bean.UserBean;
import com.homework.service.QuestionService;

public class QuestionAddAction {
	
	private QuestionService questionService = new QuestionService();
	private String courseId;
	private String questionHead;
	private String questionBody;
	private String answerBody;
	private String questionId;

	public QuestionAddAction() {
		// TODO Auto-generated constructor stub
		questionService = new QuestionService();
	}

	public String getCourseId() {
		return courseId;
	}

	public void setCourseId(String courseId) {
		this.courseId = courseId;
	}

	public String getQuestionHead() {
		return questionHead;
	}

	public void setQuestionHead(String questionHead) {
		this.questionHead = questionHead;
	}

	public String getQuestionBody() {
		return questionBody;
	}

	public void setQuestionBody(String questionBody) {
		this.questionBody = questionBody;
	}
	
	public String getAnswerBody() {
		return answerBody;
	}

	public void setAnswerBody(String answerBody) {
		this.answerBody = answerBody;
	}

	public String getQuestionId() {
		return questionId;
	}

	public void setQuestionId(String questionId) {
		this.questionId = questionId;
	}

	// 发布问题
	public String questionAdd(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		// userBean
		UserBean userBean = new UserBean();
		userBean.setUserId(String.valueOf(session.getAttribute("userId")));
		// courseBean
		CourseBean courseBean = new CourseBean();
		courseBean.setCourseId(String.valueOf(this.getCourseId()));		
		// questionBean
		QuestionBean questionBean = new QuestionBean();
		questionBean.setQuestionHead(this.getQuestionHead());
		questionBean.setQuestionBody(this.getQuestionBody());
		// courseId
		int courseIdTemp = Integer.parseInt(this.getCourseId());

		questionService.questionAdd(userBean,courseBean, questionBean);
		request.setAttribute("courseId", courseIdTemp);

		return "Success";
	}
	
	// 提交回复
	public String answerAdd(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		// userBean
		UserBean userBean = new UserBean();
		userBean.setUserId(String.valueOf(session.getAttribute("userId")));
		// courseBean
		CourseBean courseBean = new CourseBean();
		courseBean.setCourseId(String.valueOf(this.getCourseId()));		
		// answerBean
		AnswerBean answerBean = new AnswerBean();
		answerBean.setAnswerBody(this.getAnswerBody());
		answerBean.setQuestionId(this.getQuestionId());
		// courseId
		int courseIdTemp = Integer.parseInt(this.getCourseId());
		// questionId
		int questionId = Integer.parseInt(this.getQuestionId());

		questionService.answerAdd(userBean,courseBean, answerBean);
		request.setAttribute("courseId", courseIdTemp);
		request.setAttribute("questionId", questionId);

		return "Success";
	}
}
